﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Agile.Engine.Data;

namespace Agile.Engine.Plus
{
    public class ExcelReader
    {
        public static DataSet Read(string uri)
        {
            var dc = DataContext.ExcelContext(uri);
            var ds = new DataSet();
            var sql = "SELECT * FROM [{0}$]";
            var tables = dc.DefaultModel.QueryTable(CommandType.Text, "SYS_TABLES", string.Format(sql, "SYS_TABLES"));

            foreach (var r in tables.AsEnumerable().AsParallel())
            {
                var str = r[0].ToString();

                var t = dc.DefaultModel.QueryTable(
                    CommandType.Text,
                    str.ToString(),
                    string.Format(sql, str));
                ds.Tables.Add(t);
            }

            ds.Tables.Add(tables);
            return ds;
        }
    }
}
